{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T13:45:24.928746Z",
     "start_time": "2025-08-18T13:45:19.865271Z"
    }
   },
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "import os\n",
    "chatLLM = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    api_key=os.getenv(\"DASH_SCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model=\"qwen-plus\",\n",
    "    temperature=0.8,\n",
    "    max_tokens=60,\n",
    ")\n",
    "\n",
    "template = \"\"\"您是一位专业的鲜花店文案撰写员。\\n\n",
    "对于售价为 {price} 元的 {flower_name} ，您能提供一个吸引人的简短描述吗？\"\"\"\n",
    "\n",
    "prompt = PromptTemplate.from_template(template)\n",
    "\n",
    "# 打印LangChain提示模板的内容\n",
    "print(prompt)\n",
    "\n",
    "input = prompt.format(flower_name=[\"玫瑰\"], price='50')\n",
    "\n",
    "output = chatLLM.invoke(input)\n",
    "print(output.content)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input_variables=['flower_name', 'price'] input_types={} partial_variables={} template='您是一位专业的鲜花店文案撰写员。\\n\\n对于售价为 {price} 元的 {flower_name} ，您能提供一个吸引人的简短描述吗？'\n",
      "当然可以！以下是一个吸引人的简短鲜花文案：\n",
      "\n",
      "**「50元，让一朵玫瑰说出你的心动。」**\n",
      "\n",
      "如果需要更具体风格（如浪漫、文艺、俏皮、送女友/送朋友等场景），我也可以为您进一步优化文案。\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "c0069655fa4ce773"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
